package com.hui.xiao.xiaohui.ball;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hui.xiao.xiaohui.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Random;
import java.util.RandomAccess;

/**
 * Created by Administrator on 2016/5/26.
 */
public class SDStore extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sd_store);

        final EditText nameText = (EditText) findViewById(R.id.name);
        final EditText passwordText = (EditText) findViewById(R.id.password);

        Button ok = (Button) findViewById(R.id.ok);
        Button show = (Button) findViewById(R.id.show);
        Button quit = (Button) findViewById(R.id.quit);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = nameText.getText().toString() + "," + passwordText.getText().toString();
                write(word);
                Toast.makeText(SDStore.this, "写入机密成功,机密是:" + word, Toast.LENGTH_SHORT).show();
            }
        });
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = read();
                if (word == null) {
                    Toast.makeText(SDStore.this, "读取失败,先写入", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(SDStore.this, SDStoreShow.class);
                intent.putExtra("word", word);
                startActivity(intent);
            }
        });
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public String read() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File sdCardDir = Environment.getExternalStorageDirectory();
            try {
                FileInputStream fileInputStream = new FileInputStream(sdCardDir.getCanonicalPath() + "/test.txt");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                StringBuilder stringBuilder = new StringBuilder("");
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                bufferedReader.close();
                return stringBuilder.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public void write(String content) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File sdCardDir = Environment.getExternalStorageDirectory();
            try {
                File targetFile = new File(sdCardDir.getCanonicalPath() + "/test.txt");
                RandomAccessFile randomAccessFile = new RandomAccessFile(targetFile, "rw");
                randomAccessFile.write(content.getBytes());
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
